'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会成员列表
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var PlayerManager = require('../../../../libs/playerManager');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionGroupService = require('../../../../services/unionGroupService');
var onlineService = require('../../../../services/onlineService.js');

/**
 * 公会成员列表
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_msg;
    var role_model;
    var union_id;
    var union_user_list = [];
    var union_group_model;
    var now_union_user_num = 0;
    var union_user_max = 0;
    var apply_state = 0;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'friend', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();

                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                var union_group_list = union_group_model.getUnionUserList();
                now_union_user_num = union_group_list.length;
                union_user_max = unionGroupConf.getUnionMemberLimitNum(union_group_model.getUnionLv());

                // 如果时间隔天清空加入公会申请时间
                union_group_model.removeJoinTime(role_id);

                // 红点审核判断
                if (!!union_group_model.isBoss(role_id) && !!union_group_model.apply_list.length) {
                    apply_state = 1;
                }

                var friend_model = player_manager.friend;
                var friend_list = friend_model.getFriendList();
                _getUnionUserMsg1(union_group_list, union_group_model, friend_list, cb);
            },
            function (list, cb) {
                union_user_list = list;
                cb();
            }

        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK,
                result: {
                    member_list: union_user_list,
                    now_union_user_num: now_union_user_num,
                    union_user_max: union_user_max,
                    apply_state: apply_state
                }
            });
        }
    );
};
// 获取成员信息
var _getUnionUserMsg1 = function (union_group_list, union_group_model, friend_list, callback) {
    var union_member_list = [];
    var item;
    async.forEach(union_group_list, function (union_item, cb) {
        _getUnionUserMsg(union_item.role_id, union_group_model, friend_list, function (err, union_member) {
            if (!!err) {
                console.error(err);
                return cb();
            }
            if (!union_member) {
                item = [];
            }

            if (union_member.length > 0) {
                item = union_member;
                union_member_list.push(item[0]);
            }

            cb();
        });
    }, function (err) {
        if (!!err) {
            console.error(err);
        }
        callback(null, union_member_list);
    });
};

// 获取成员信息
var _getUnionUserMsg = function (role_id, union_group_model, friend_list, callback) {
    var user_item_msg = [];
    var position = 3;
    var is_friend = 0;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(
                    ['role', 'friend', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var union_group_list = union_group_model.getUnionUserList();
                _.some(union_group_list, function (group_item) {
                    if (group_item.role_id == role_id) {
                        position = group_item.position;
                        return true;
                    }
                });

                cb();
            },
            function (cb) {
                // 判断是否玩家在线
                onlineService.checkRoleOnline(role_id, cb);
            },

            function (is_online, cb) {
                var role_model = player_manager.role;
                var union_task_model = player_manager.unionTask;

                if (_.indexOf(friend_list, role_id) == -1) {
                    is_friend = 0;
                } else {
                    is_friend = 1;
                }

                var user_is_online = 0;
                if (!!is_online) {
                    user_is_online = 1;
                } else {
                    user_is_online = 0;
                }
                user_item_msg.push({
                    role_id: role_id,
                    level: role_model.getLv(),
                    vip: role_model.getVip(),
                    main_power: role_model.main_power,
                    name: role_model.name,
                    is_online: user_is_online,
                    position: position,
                    job_type: role_model.getJob(),
                    is_friend: is_friend,
                    history_reputation: union_task_model.history_reputation_total
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            callback(null, user_item_msg);
        }
    );
};
